<template>
	<view>
		<view class="container">
			<!-- 日期 -->
			<view class="date flex-between">
				<view class="li" v-for="(item, index) in date" :key="index" :class="[index == curIndex ? 'li-c' : '']" @click="chDate(index)">{{item}}</view>
			</view>
			<!-- 面板1 -->
			<view class="panel" v-if="curPanel === 0">
				<view class="sales flex-between">
					<view class="lef">
						销售额
						<view class="price">￥{{prodData.subData.alipayTotalPrice}}</view>
					</view>
					<view class="rig tc" @click="curPanel = 1">切换订单</view>
				</view>
				<view class="ul flex-between">
					<view class="li ">
						<view class="lef">
							全部佣金
							<view class="price">￥{{prodData.subData.totleCommission}}</view>
						</view>
						<view class="rig">
							结算佣金
							<view class="price">￥{{prodData.subData.settleCommission}}</view>
						</view>
					</view>
					<view class="li">
						<view class="lef">
							付款佣金
							<view class="price">￥{{prodData.subData.payCommission}}</view>
						</view>
						<view class="rig">
							失效佣金
							<view class="price">￥{{prodData.subData.invalidCommission}}</view>
						</view>
					</view>
				</view>
			</view>
			<!-- 面板2 -->
			<view class="panel" v-else>
				<view class="sales flex-between">
					<view class="lef">
						销售额
						<view class="price">￥{{prodData.subData.alipayTotalPrice}}</view>
					</view>
					<view class="rig tc" @click="curPanel = 0">切换佣金</view>
				</view>
				<view class="ul flex-between">
					<view class="li">
						<view class="lef">
							全部单数
							<view class="price">{{prodData.subData.orderNum}}</view>
						</view>
						<view class="rig">
							结算订单
							<view class="price">{{prodData.subData.settleOrderNum}}</view>
						</view>
					</view>
					<view class="li">
						<view class="lef">
							付款订单
							<view class="price">{{prodData.subData.payOrderNum}}</view>
						</view>
						<view class="rig">
							失效订单
							<view class="price">{{prodData.subData.invalidOrderNum}}</view>
						</view>
					</view>
				</view>
			</view>
			<!-- 过滤 -->
			<view class="filter flex-between" :class="{ceil : isShowSearch}">
				<view class="flex-between" @click="toPath('taobaoId')">
					<view class="text-over pid">{{filterObj.taobaoId.label}}</view>
					<text class="iconfont">&#xe618;</text>
				</view>
				<Filter @open="timeShow = true" @filter="filter" type="time" :show="timeShow"></Filter>
				<Filter @open="orderShow = true" @filter="filter" type="order" :show="orderShow"></Filter>
				<view class="flex-between" @click="toPath('positionId')">
					<view class="text-over pid">{{filterObj.positionId.label}}</view>
					<text class="iconfont">&#xe618;</text>
				</view>
			</view>
		</view>
		<!-- 列表 -->
		<view class="list">
			<view class="item" v-for="(item,index) in prodData.list" :key="index" @click="toDetail(item)">
				<view class="top flex-between">
					<image :src="item.itemImg"></image>
					<view class="rig">
						<view class="title text-over">{{item.itemTitle}}</view>
						<view class="time flex-between">
							<text v-if="item.tkStatus === 12">{{switchOrder(item.tkStatus)}}</text>
							<text v-if="item.tkStatus === 3" style="color: #FE6E00;">{{switchOrder(item.tkStatus)}}</text>
							<text v-if="item.tkStatus === 13" style="color: #999999;">{{switchOrder(item.tkStatus)}}</text>
							<view class="tiem-rig">{{formatDate(item.tkPaidTime)}}付款</view>
						</view>
					</view>
				</view>
				<view class="cen flex-between">
					<view class="li">
						付款金额
						<view>￥{{item.alipayTotalPrice}}</view>
					</view>
					<view class="li">
						提成比
						<view>{{item.incomeRate}}%</view>
					</view>
					<view class="li">
						预估收入
						<view>￥{{item.pubSharePreFee}}</view>
					</view>
					<view class="li">
						下单时长
						<view>{{hesitateTime(item.clickTime, item.tkPaidTime)}}</view>
					</view>
				</view>
				<view class="bom flex-between">
					<view class="user flex-between">
						<image src="../../static/my/union.png"></image>
						{{item.account}}
					</view>
					<text class="name">运营人：{{item.staffName || '无'}}</text>
				</view>
			</view>
		</view>
		<!-- 加载文案 -->
		<LoadMore :loading="loading" :finished="finished" :empty="empty" :show="true" @open="authShow = true"></LoadMore>
		<!-- 扫码客服弹窗 -->
		<van-dialog class="custom-dialog" use-slot title="授权账号授权" :show="authShow" :showConfirmButton="false"
		 closeOnClickOverlay @close="closeAuth">
			<view class="vip-container">
				<image src="../../static/my/option.gif" mode=""></image>
				<view class="step">
					<view>授权步骤：</view>
					<view>步骤1.首先点击下方<text>【复制授权链接】</text>按钮。</view>
					<view>步骤2.用<text>浏览器打开</text>已经复制好的链接。</view>
					<view>步骤3.登录要授权的联盟账号密码，点击【授权并登陆】，</view>
					<view>即为授权成功。</view>
				</view>
				<view class="copy" :class="[isCopy ? 'copy-c' : '']" @click="copy">{{isCopy ? '复制成功，前往浏览器打开链接授权' : '复制授权链接'}}</view>
				<view class="last" @click="authed">我已授权</view>
			</view>
		</van-dialog>

		<!-- 回到顶部 -->
		<view class="scrollTop" :class="[{ show: isShowScrollTop }]" @tap="backTop">
			<text class="iconfont">&#xe7ed;</text>
			<text class="text">置顶</text>
		</view>
	</view>
</template>

<script>
	import moment from '@/utils/moment'
	import {
		dateFormat,
		formatDate,
		hesitateTime
	} from '../../utils'
	import Filter from '@/components/Filter.vue'
	import LoadMore from '@/components/LoadMore.vue'
	export default {
		components: {
			Filter,
			LoadMore
		},
		data() {
			return {
				date: ['今天', '昨天', '最近七天', '最近30天', '本月'],
				isShowSearch: false, //吸顶
				isShowScrollTop: false,
				timeShow: false,
				orderShow: false,
				authShow: false,
				isCopy: false,
				curIndex: 0,
				curPanel: 0,
				params: {
					page: 1,
					size: 10,
					order: 'tkPaidTime',
					sort: 'desc',
					startTime: moment().hour(0).minute(0).second(0).format('YYYY-MM-DD HH:mm:ss'),
					endTime: moment().format('YYYY-MM-DD HH:mm:ss'),
					timeType: 1,
					taobaoId: '',
					positionId: '',
					staffId: '',
					tkStatus: ''
				},
				// 获取到的数据
				prodData: {
					subData: {
						alipayTotalPrice: 0,
						orderNum: 0,
						validCommission: 0,
						validOrderNum: 0,
						payCommission: 0,
						payOrderNum: 0,
						settleCommission: 0,
						settleOrderNum: 0,
						invalidCommission: 0,
						invalidOrderNum: 0,
						totleCommission: 0,
					},
					list: [],
					pagination: null
				},
				loading: false,
				finished: false,
				empty: false,
				// 过滤类型
				filterType: '', 
				filterObj: {
					positionId: {
						value: '',
						label: '推广位'
					},
					taobaoId: {
						value: '',
						label: '联盟账号'
					}
				}				
			};
		},
		onLoad() {
			this.getTime()
		},
		onShow() {
			let data = uni.getStorageSync(this.filterType)
			if (!data) { // 按钮返回键什么都没选
				this.params[this.filterType] = this.params[this.filterType]
				this.filterObj[this.filterType] = this.filterObj[this.filterType]
			} else {
				this.params[this.filterType] = data.value
				this.filterObj[this.filterType] = data
			}			
			setTimeout(() => {
				this.clear()
				this.getData()
			}, 300)
		},
		// 上拉加载
		onReachBottom() {
			if (this.finished) return
			this.params.page++
			this.loading = true
			this.getData()
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.clear()
			this.getData()
		},
		onPageScroll(e) {
			if (e.scrollTop > 260) {
				this.isShowSearch = true;
			} else {
				this.isShowSearch = false;
			}
			if (e.scrollTop > 350) {
				this.isShowScrollTop = true;
			} else {
				this.isShowScrollTop = false;
			}
		},

		methods: {
			formatDate,
			hesitateTime,
			// 获取数据
			getData() {
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				this.$service.order.orderStatistics(this.params).then((res) => {
					this.prodData.list = this.prodData.list.concat(res.list)
					this.prodData.pagination = res.pagination
					this.prodData.subData = res.subData
				
					this.loadMore(res)
					uni.hideLoading();
					uni.stopPullDownRefresh();
				}).catch((err) => {
					uni.hideLoading();
					uni.stopPullDownRefresh();
				})
				uni.removeStorageSync('pid')
			},
			//列表加载的一些逻辑判断
			loadMore(res) {
				if (res.list.length < this.params.size) this.finished = true
				if (this.prodData.list.length === 0) this.empty = true
				this.loading = false
			},
			toPath(val) {
				this.filterType = val
				uni.navigateTo({
					url: `/pages/common/check?type=${val}&values=${this.params[val] || 'all'}`
				})
			},
			// 过滤
			filter(item, type) {
				switch (type) {
					case 'time':
						this.params.timeType = item.value
						this.params.order = item.order
						this.timeShow = false
						break;
					case 'order':
						this.params.tkStatus = item.value
						this.orderShow = false
						break;
					case 'shade':
						this.timeShow = false
						this.orderShow = false
						return
				}
				this.clear()
				this.getData()
			},
			getTime(val) {
				this.params = {
					...this.params,
					...dateFormat(val)
				}
			},
			// 选择日期
			chDate(index) {
				this.curIndex = index
				this.getTime(index)
				this.clear()
				this.getData()
			},
			//3：订单结算，12：订单付款， 13：订单失效，14：订单成功
			// 初始化订单状态
			switchOrder(val) {
				switch (val) {
					case 3:
						return '订单结算';
					case 12:
						return '订单付款';
					case 13:
						return '订单失效';
					case 14:
						return '订单成功';
				}
			},
			// 清楚数据
			clear() {
				this.params.page = 1
				this.prodData.list = []
				this.loading = false
				this.finished = false
				this.empty = false
			},
			// 获取所有阿里妈妈
			getTaobao() {
				this.$service.common.taobao().then((res) => {
					res.forEach((item, index) => {
						let obj = {}
						obj.id = item.id
						obj.account = item.account
						this.tbList.push(obj)
					})
				}).catch((err) => {})
			},
			// 跳转详情页
			toDetail(item) {
				item = encodeURIComponent(JSON.stringify(item))
				uni.navigateTo({
					url: `/pages/index/detail?info=${item}`
				})
			},
			// 关闭授权
			closeAuth() {
				this.authShow = false
				this.isCopy = false
			},
			// 我已授权
			authed() {
				this.isCopy = false
				this.authShow = false
				this.clear()
				this.getData()
			},
			// 获取授权连接
			copy() {
				let that = this
				this.$service.taobao.getLink({
					type: 'web'
				}).then((res) => {
					uni.setClipboardData({
						data: res,
						success: function() {
							that.isCopy = true
						}
					})
				}).catch((err) => {

				})
			},
			// 回到顶部
			backTop() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				})
			}
		}
	};
</script>
<style lang="scss" scoped>
	.container {
		background-color: white;
		padding: 0 24rpx;

		.date {
			padding: 24rpx 0;

			.li {
				font-szie: 20rpx;
				width: 128rpx;
				height: 48rpx;
				border-radius: 180rpx;
				background-color: #FFECE5;
				color: $main-color;
				text-align: center;
				line-height: 48rpx;
			}

			.li-c {
				background-color: $main-color;
				color: white;
			}
		}

		.panel {
			width: 702rpx;
			height: 404rpx;
			border-radius: 8rpx;
			padding: 30rpx 40rpx 40rpx 40rpx;
			font-size: 20rpx;
			color: white;
			overflow: hidden;
			background-color: $main-color;

			.sales {
				.price {
					font-size: 48rpx;
					font-weight: bold;
				}

				.rig {
					width: 128rpx;
					height: 48rpx;
					border-radius: 24rpx;
					font-size: 20rpx;
					background-color: white;
					line-height: 48rpx;
					color: $main-color;
				}
			}

			.ul {
				margin-top: 52rpx;

				.li {
					view {
						font-size: 20rpx;
					}

					.price {
						font-size: 36rpx;
						font-weight: bold;
					}

					.rig {
						margin-top: 34rpx;
					}
				}

				.bom {
					margin-top: 34rpx;
				}
			}
		}

		.filter {
			padding: 50rpx 24rpx 16rpx 24rpx;
			background-color: white;

			.icon {
				width: 24rpx;
				height: 24rpx;
				transition: all 0.5s;
			}

			.pid {
				max-width: 3.8rem
			}
		}

		.ceil {
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
		}
	}

	.list {
		padding: 22rpx 24rpx;

		.item {
			background-color: white;
			padding: 20rpx;
			border-radius: 8rpx;
			margin-bottom: 24rpx;

			.top {
				align-items: flex-start;

				image {
					width: 96rpx;
					height: 96rpx;
				}

				.rig {
					flex: 1;
					padding-left: 12rpx;

					.title {
						font-weight: bold;
					}

					.time {
						margin-top: 10rpx;

						text {
							font-size: 20rpx;
							color: #0098FE;
						}

						view {
							font-size: 20rpx;
							color: #999999;
						}
					}
				}
			}

			.cen {
				padding: 22rpx 14rpx 26rpx 28rpx;
				border-bottom: 2rpx solid #F5F5F5;

				.li {
					text-align: center;
					font-size: 20rpx;
					color: #999999;

					view {
						margin-top: 4rpx;
						color: #333333;
					}
				}
			}

			.bom {
				padding: 18rpx 0 6rpx 0;

				image {
					width: 40rpx;
					height: 40rpx;
					border-radius: 50%;
					vertical-align: middle;
					margin-right: 16rpx;
				}

				text,
				.user {
					color: #707070;
				}
			}
		}
	}

	.list view:last-child {
		margin-bottom: 0;
	}

	/deep/ .custom-dialog {
		.van-dialog__header {
			padding-top: 28rpx;
		}

		.vip-container {
			image {
				width: 524rpx;
				height: 476rpx;
				margin: 0 auto;
				display: block;
				margin-bottom: 22rpx;
			}

			.step {
				padding-left: 38px;

				view {
					color: #666666;
					line-height: 1.5em;
				}

				text {
					color: #0C5DFF;
				}
			}

			.copy {
				width: 428rpx;
				height: 72rpx;
				border-radius: 42px;
				background-color: #0C5DFF;
				color: white;
				line-height: 72rpx;
				text-align: center;
				margin: 0 auto;
				margin-top: 20rpx;
				margin-bottom: 18rpx;
			}

			.copy-c {
				background-color: #E6EEFF;
				color: #333333;
			}

			.last {
				color: #0C5DFF;
				text-align: center;
				padding-bottom: 42rpx;
			}
		}
	}
</style>
